﻿
using System;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;

//Mono Comp
[Serializable]
public class MeshItem
{
    [Tooltip("不能重复")]
    public uint id;
    public string name;
    public GameObject prefab;
}
[Serializable]
public class EffectItem
{
    [Tooltip("不能重复")]
    public uint id;
    public string name;
    public GameObject prefab;
    public float lifetime;
}
[Serializable]
public class Dummy
{
    [Tooltip("不能重复")]
    public uint id;
    public string name;
    public Vector3 position;
    public Vector3 dir;
    [Tooltip("如果配置了fromObj,则Postion和 dir 会自动从这里取")]
    public Transform fromObj;//如果配置了fromObj,则Postion和 dir 会自动从这里取
}
//初始化配置组件，放在SubScene里
public class comp_initconfig : MonoBehaviour
{
    public MeshItem[] MeshItems;
    public Dummy[] Dummys;
    public EffectItem[] Effects;
}
//ECS 组件，用来记录配置，主要是位置与预设

public struct dotsbuf_mesh : IBufferElementData
{
    public uint id;
    public FixedString128Bytes name;
    public Entity prefab;
}
public struct dotsbuf_dummy : IBufferElementData
{
    public uint id;
    public float3 position;
    public float3 dir;
}
public struct dotsbuf_effect : IBufferElementData
{
    public uint id;
    public FixedString128Bytes name;
    public Entity prefab;
    public float lifetime;
}

public struct dotscomp_InitConfig : IComponentData
{
    public int version;
    public FixedString128Bytes name;
}
public struct dotscomp_StartTag : IComponentData
{

}
//Bake Convert Dots Comp->mono comp
public class SimpleBaker : Baker<comp_initconfig>
{
    public override void Bake(comp_initconfig authoring)
    {
        //return entiry for this object;
        var entity = GetEntity(TransformUsageFlags.Dynamic);
        var init = new dotscomp_InitConfig()
        {
            version = 1,
            name = "somegame."
        };
        AddComponent(entity, init);

        var bufmesh = this.AddBuffer<dotsbuf_mesh>(entity);
        if (authoring.MeshItems != null)
            foreach (var item in authoring.MeshItems)
            {
                var buf = new dotsbuf_mesh();

                buf.prefab = GetEntity(item.prefab, TransformUsageFlags.Dynamic);
                buf.name = item.name;
                buf.id = item.id;
                bufmesh.Add(buf);

            }
        var bufeffect = this.AddBuffer<dotsbuf_effect>(entity);
        if (authoring.Effects != null)
            foreach (var item in authoring.Effects)
            {
                var buf = new dotsbuf_effect();

                buf.prefab = GetEntity(item.prefab, TransformUsageFlags.Dynamic);
                buf.name = item.name;
                buf.id = item.id;
                buf.lifetime = item.lifetime;
                bufeffect.Add(buf);

            }
        var bufdummy = this.AddBuffer<dotsbuf_dummy>(entity);
        if (authoring.Dummys != null)
            foreach (var item in authoring.Dummys)
            {
                var buf = new dotsbuf_dummy();

                buf.id = item.id;
                if (item.fromObj != null)
                {
                    buf.position = item.fromObj.transform.position;
                    buf.dir = item.fromObj.transform.forward;
                }
                else
                {
                    buf.position = item.position;
                    buf.dir = item.dir;
                }
                bufdummy.Add(buf);
            }
        Debug.LogWarning("[Bake]comp_initconfig=>dotscomp_InitConfig");
    }
}
